package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //创建分页对象
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //根据name模糊分页查询套餐
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        page = setmealMapper.selectPage(page, wrapper);

        //2.遍历套餐列表 使用分页查询到的数据都保存在Records集合中
        List<Setmeal> setmealList = page.getRecords();
        //遍历之前先进行非空判断 避免空指针
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                //根据套餐表的CategoryId获取分类表中对应的分类名称 再封装到setmeal对象中
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());
                //根据套餐表的id去中间表查询具体的菜品信息,并将菜品信息封装到setmeal对象中
                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SetmealDish::getSetmealId, setmeal.getId());
                List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper1);
                setmeal.setSetmealDishes(setmealDishes);
            }
        }
        return page;
    }

    @Override
    public void save(Setmeal setmeal) {
        //1.基本信息直接保存到套餐表
        setmealMapper.insert(setmeal);
        //2.保存套餐下的具体菜品信息
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            //遍历前端传入的菜品信息
            for (SetmealDish setmealDish : setmealDishes) {
                //设置套餐id
                setmealDish.setSetmealId(setmeal.getId());
                //保存到中间表
                setmealDishMapper.insert(setmealDish);

            }
        }
    }

    @Override
    public void delete(Long[] ids) {
        //1.判断要删除的套餐是否有启售状态的
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getStatus, 1)//Status为1是启售
                .in(Setmeal::getId, ids);
        Integer count = setmealMapper.selectCount(wrapper);
        if (count > 0) {
            //如果有套餐是启售状态,抛出异常由全局异常处理器处理
            throw new CustomException("当前套餐处于在售状态,无法删除");
        }
        for (Long id : ids) {
            //删除中间表
            LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SetmealDish::getSetmealId, id);
            setmealDishMapper.delete(wrapper1);
            //删除套餐表
            setmealMapper.deleteById(id);
        }

    }

    @Override
    public Setmeal findById(Long id) {
        //回显套餐基本信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //回显套餐分类名称
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getId, setmeal.getCategoryId());
        Category category = categoryMapper.selectOne(wrapper);
        setmeal.setCategoryName(category.getName());


        //回显中间表
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper1);

        //获取每一条中间表
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {
                LambdaQueryWrapper<Dish> wrapper2 = new LambdaQueryWrapper<>();
                wrapper2.eq(Dish::getId, setmealDish.getDishId());
                Dish dish1 = dishMapper.selectOne(wrapper2);
                setmealDish.setImage(dish1.getImage());
//            根据中间表的id获取到中间表对应的菜品表
//            Dish dish = dishMapper.selectById(setmealDish.getDishId());
//            将菜品表的图片赋值给中间表
//            setmealDish.setImage(dish.getImage());

            }
            setmeal.setSetmealDishes(setmealDishes);
        }
        return setmeal;
    }

    @Override
    public List<SetmealDish> findBySetmealId(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        //中间表
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper1);
        //获取每一条中间表
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {
         /*       LambdaQueryWrapper<Dish> wrapper2 = new LambdaQueryWrapper<>();
                wrapper2.eq(Dish::getId, setmealDish.getDishId());
                Dish dish1 = dishMapper.selectOne(wrapper2);
                setmealDish.setImage(dish1.getImage());*/
                // 根据中间表的id获取到中间表对应的菜品表
                Dish dish = dishMapper.selectById(setmealDish.getDishId());
                //将菜品表的图片赋值给中间表
                setmealDish.setImage(dish.getImage());
            }
        }
        return setmealDishes;
    }

    @Override
    public void update(Setmeal setmeal) {
        //修改套餐基本信息
        setmealMapper.updateById(setmeal);
        //修改菜品表信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(wrapper);
        //将修改后的菜品信息保存到中间表
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public void updateStatus(Integer status, Long[] ids) {
        //update Setmeal set status =#{status} where id in ins
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealMapper.update(setmeal, wrapper);
    }

    @Override
    public List<Setmeal> findBycategoryId(Long categoryId, Integer status) {
        //回显套餐表
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, status);
        return setmealMapper.selectList(wrapper);

    }
}
